{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OpenCV VERSION (should be 3.1.0 or later, with nonfree modules installed!): 3.1.0\n"
     ]
    }
   ],
   "source": [
    "import visual_bow as bow\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.grid_search import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38 positive, 38 negative images selected (out of 9106 negatives total)\n"
     ]
    }
   ],
   "source": [
    "positive_folder = 'panda'\n",
    "imgs = bow.binary_labeled_img_from_cal101(positive_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "generating SIFT keypoints for 76 images\n",
      "Train-test split: 15 training rows, 61 test rows\n",
      "10559 descriptors before clustering\n",
      "Clustering on training set to get codebook of 750 words\n",
      "done clusetering\n"
     ]
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test, kmeans = bow.gen_bow_features(imgs, test_train_ratio=0.8, K_clusters=750)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.73770491803278693"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c_vals = [0.0001, 0.01, 0.1, 1, 5, 10, 100, 1000]\n",
    "\n",
    "param_grid = [\n",
    "  {'C': c_vals, 'kernel': ['linear']},\n",
    "  {'C': c_vals, 'gamma': [0.01, 0.001, 0.0001], 'kernel': ['rbf']},\n",
    " ]\n",
    "\n",
    "svc = GridSearchCV(SVC(), param_grid, n_jobs=-1)\n",
    "svc.fit(X_train, y_train)\n",
    "svc.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=0.01, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape=None, degree=3, gamma='auto', kernel='linear',\n",
       "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "  tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svc.best_estimator_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# We have our estimator, let's try on a new picture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kanye_glasses.jpeg ['False']\n",
      "kanye_glasses2.jpeg ['False']\n",
      "more_pandas/0001.jpeg ['True']\n",
      "101_ObjectCategories/brontosaurus/image_0001.jpg ['False']\n",
      "101_ObjectCategories/brontosaurus/image_0002.jpg ['False']\n",
      "101_ObjectCategories/dalmatian/image_0001.jpg ['True']\n",
      "101_ObjectCategories/dalmatian/image_0002.jpg ['False']\n"
     ]
    }
   ],
   "source": [
    "for img_path in ['kanye_glasses.jpeg', \n",
    "                 'kanye_glasses2.jpeg', \n",
    "                 'more_pandas/0001.jpeg', \n",
    "                 '101_ObjectCategories/brontosaurus/image_0001.jpg',\n",
    "                 '101_ObjectCategories/brontosaurus/image_0002.jpg',\n",
    "                 '101_ObjectCategories/dalmatian/image_0001.jpg',\n",
    "                 '101_ObjectCategories/dalmatian/image_0002.jpg'\n",
    "                ]:\n",
    "    print img_path, svc.predict(bow.img_to_vect(img_path, kmeans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['pickles/panda/panda_svc.pickle',\n",
       " 'pickles/panda/panda_svc.pickle_01.npy',\n",
       " 'pickles/panda/panda_svc.pickle_02.npy',\n",
       " 'pickles/panda/panda_svc.pickle_03.npy',\n",
       " 'pickles/panda/panda_svc.pickle_04.npy',\n",
       " 'pickles/panda/panda_svc.pickle_05.npy',\n",
       " 'pickles/panda/panda_svc.pickle_06.npy',\n",
       " 'pickles/panda/panda_svc.pickle_07.npy',\n",
       " 'pickles/panda/panda_svc.pickle_08.npy',\n",
       " 'pickles/panda/panda_svc.pickle_09.npy',\n",
       " 'pickles/panda/panda_svc.pickle_10.npy',\n",
       " 'pickles/panda/panda_svc.pickle_11.npy']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Uncomment to pickle the best SVC classifier\n",
    "###########\n",
    "# from sklearn.externals import joblib\n",
    "# joblib.dump(svc.best_estimator_, 'pickles/panda/panda_svc.pickle')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
